<?php
namespace Home\Controller;
use Common\Common\Classes\Message;
use Common\Common\PHPExcel\Classes\PHPExcel;
use Home\Model\QuestionService;
use Home\Model\QuestionSetService;
use Home\Model\StudentQuestionService;
use Home\Model\StudentResultService;
use Home\Model\StudentService;
use Think\Controller;

//本控制器主要用于查看学生的答题情况
//显示出学生所做的每一题,有学生答案有正确答案
class StuQuizController extends HomeController{
    //---------------------------------跳转到学生答题的页面(试卷结果)-------------------------------------
    public function quizResult(){       //需要一个学生id和一个试卷id
        $_POST["qus_id"] = $_GET["qd"];
        $qs_sql = new QuestionSetService();
        $quiz_field = array("qus_name");
        $quiz = $qs_sql->getQuizInfo($quiz_field);
        $check = $qs_sql->checkPower();
        if($check->status == 1) {
            $this->assign("stu_id",$_GET["sd"]);
            $this->assign("qus_id",$_GET["qd"]);
            $this->assign("qus_name",$quiz["qus_name"]);
            //可能需要返回某些信息..后续加
            $this->display();
        }
        else{
            $this->error($check->message);
        }
    }
    //---------------------------------获得学生的答题情况(做题情况)--------------------------------------
    public function getStuQuizResult(){         //获得学生做试卷的结果..需要学生id  stu_id  和试卷id  qus_id
        if(IS_AJAX) {
            $qus_sql = new QuestionSetService();
            $check = $qus_sql->checkPower();        //检测权限
            if ($check->status == 1) {
                $stu_sql = new StudentService();
                $stu_field = array("stu_id","stu_number","stu_name");
                $d["student"] = $stu_sql->getStuInfo($stu_field);
                unset($d["student"]["stu_email"]);
                unset($d["student"]["sex"]);
                unset($_POST["stu_id"]);
                $quiz_field = array("qus_name");
                $d["quiz"] = $qus_sql->getQuizInfo($quiz_field);
                $_POST["stu_id"] = $d["student"]["stu_id"];
                $sr_sql = new StudentResultService();
                $quizResult = $sr_sql->getStuQuizResult();      //得到学生该试卷的成绩
                $d["student"]["stu_score"] = $quizResult["stu_score"];
                $que_sql = new QuestionService();
                $que_field = array("que_id","que_type","que_content","que_num","que_item1","que_item2","que_item3","que_item4","que_item5",
                    "que_item6","que_item7","que_item8","que_answer","que_score");
                $d["questions"] = $que_sql->unsetQusetion($que_sql->getQuizQuestion($que_field));
                unset($_POST["qus_id"]);
                $sq_sql = new StudentQuestionService();
                for($i = 0;$i<count($d["questions"]);$i++){
                    $_POST["que_id"] = $d["questions"][$i]["que_id"];
                    $result_field = array("stu_answer","is_correct");
                    $stu_answer = $sq_sql->getStuQuizResult($result_field);
                    $d["questions"][$i]["stu_answer"] = $stu_answer["stu_answer"];
                    $d["questions"][$i]["is_correct"] = $stu_answer["is_correct"];
                }
                $data = new Message(1, "获取信息成功!", $d);
            } else {
                $data = $check;
            }
            $this->ajaxReturn($data);
        }
    }
}